package common.utilities;

import java.net.*;
import java.io.*;
import org.apache.catalina.cluster.mcast.McastService;
import org.apache.catalina.cluster.Member;
import common.logging.WebPortalLogger;

public class ServerUtil {

   public static String clusterNodeHost = "127.0.0.1";
   public static int clusterNodePort = 4001;
   public static String strURL = "http://127.0.0.1:9080/clusterapp/test.jsp";

	public ServerUtil() {
	}

	public boolean isServerAlive(String host, int port) {
		boolean isAlive = false;
		long t1 = System.currentTimeMillis();
//		WebPortalLogger.info("Start Time: " + t1);

      // Get the cluster nodes
      try {
         String tcpListHost = "127.0.0.1";
         String tcpListPort = "4009";
         McastService service = new McastService();
         java.util.Properties p = new java.util.Properties();
         p.setProperty("mcastAddress","228.0.0.4");
         p.setProperty("mcastPort","45564");
         p.setProperty("bindAddress","localhost");
         p.setProperty("memberDropTime","3000");
         p.setProperty("msgFrequency","500");
         p.setProperty("tcpListenHost",tcpListHost);
         p.setProperty("tcpListenPort",tcpListPort);
         service.setProperties(p);

         service.start();
//         Thread.currentThread().sleep(1*1000);
         Thread.currentThread().sleep(500);

         Member[] members = service.getMembers();
         int m = members.length;
         WebPortalLogger.info("# of nodes in the cluster : " + m);
         WebPortalLogger.info("Check the cluster nodes for [host=" + host + ",port=" + port + "]");
         for (int i=0; i<members.length; i++) {
            WebPortalLogger.info("Cluster node " + (i+1) + ": [host=" + members[i].getHost() + ",port=" + members[i].getPort() + "]");
            if (host.equals(members[i].getHost()) && port == members[i].getPort()) {
               isAlive = true;
               break;
            }
         }
         // Stop the service
         service.stop();
      } catch (Exception e) {
         e.printStackTrace();
      }

		long t2 = System.currentTimeMillis();
//		WebPortalLogger.info("End Time: " + t2);
		WebPortalLogger.info("Time taken to check connection: " + (t2-t1) + " ms.");
		return isAlive;
	}

   public boolean isServerAlive(String pStrURL) {
      boolean isAlive = false;
      long t1 = System.currentTimeMillis();
//		WebPortalLogger.info("Start Time: " + t1);

      try {
         URL url = new URL(pStrURL);
         BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
         String inputLine;
         while ((inputLine = in.readLine()) != null) {
            //WebPortalLogger.info(inputLine);
         }
         WebPortalLogger.info("**  Connection successful..  **");
         in.close();
         isAlive = true;
      } catch (Exception e) {
//			WebPortalLogger.printStackTrace();
         WebPortalLogger.info("**  Connection failed..  **");
         isAlive = false;
      }

      long t2 = System.currentTimeMillis();
//		WebPortalLogger.info("End Time: " + t2);
      WebPortalLogger.info("Time taken to check connection: " + (t2-t1) + " ms.");
      return isAlive;
   }

	public static void main(String[] args) throws Exception {
		ServerUtil test = new ServerUtil();
      // Use McastService to verify cluster node status
		if (test.isServerAlive(clusterNodeHost, clusterNodePort)) {
			WebPortalLogger.info("**  IT'S ALIVE  **");
		} else {
			WebPortalLogger.info("**  THE SERVER IS NOT AVAILABLE  **");
		}
      // Use URL reader to verify cluster node status
      if (test.isServerAlive(strURL)) {
         WebPortalLogger.info("**  IT'S ALIVE  **");
      } else {
         WebPortalLogger.info("**  THE SERVER IS NOT AVAILABLE  **");
      }
	}
}

// End of ServerUtil.java
